<?php

$link=mysql_connect ("localhost","sun","") or die ("Can not connect to db!".mysql_error());
mysql_select_db("jcat",$link) or die ("Can not select db!".mysql_error());

echo '
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>交易商遷移穩定度</title>
</head>
<body> 
<table><tr><td>
<div id="jcat_chart"></div>
</td><td valign="top">
<FORM action="./traderMigrationSta.php" method="get">
    <P>
	<LABEL for="trader">交易商類型: </LABEL>
		<select name="trader">
			<option value="buyer_zic" '.$buyer_zic.'>買方-ZIC</option>
			<option value="buyer_gd" '.$buyer_gd.'>買方-GD</option>
			<option value="buyer_re" '.$buyer_re.'>買方-RE</option>
			<option value="buyer_zip" '.$buyer_zip.'>買方-ZIP</option>
			<option value="seller_zic" '.$seller_zic.'>賣方-ZIC</option>
			<option value="seller_gd" '.$seller_gd.'>賣方-GD</option>
			<option value="seller_re" '.$seller_re.'>賣方-RE</option>
			<option value="seller_zip" '.$seller_zip.'>賣方-ZIP</option>			
		</select><BR />
	<LABEL for="level1">實驗類型 1: </LABEL>
		<select name="level1">
			<option value="sp" '.$sp.'>專家之經營政策對交易代理人的影響評估</option>
			<option value="ac" '.$ac.'>交易代理人移動特徵分群</option>
			<option value="sc" '.$sc.'>專家代理人特徵分群</option>
			<option value="aa" '.$aa.'>自適應專家策略自主適應競爭態能力評估</option>
		</select><BR />
	<LABEL for="level2">實驗類型 2: </LABEL>
		<select name="level2">
			<option value="si" '.$si.'>單一市場</option>
			<option value="ho" '.$ho.'>同質市場</option>
			<option value="he" '.$he.'>異構市場</option>
			<option value="com" '.$com.'>競賽模式</option>
			<option value="real" '.$real.'>現實模式</option>
			<option value="GAME2008" '.$GAME2008.'>2008年競賽</option>
		</select><BR />
	<LABEL for="game">場次: (0001~9999)</LABEL>
			<input type="text" name="game" value="'.$game.'">
	<BR />
    <INPUT type="submit" value="查詢"> <INPUT type="reset">
    </P>
 </FORM>
</td></tr></table><p>';

$trader = $_GET['trader'];
$level1 = $_GET['$level1'];
$level2 = $_GET['$level2'];
$game = $_GET['$game'];

if(!$trader)$trader = 'buyer_gd';
if(!$level1)$level1 = 'sc';
if(!$level2)$level2 = 'com';
if(!$game)$game='0001';

$traderRole = substr($trader, 0,3);

if($traderRole=='buy'){
	$shout = 'BidId';
}else if($traderRole=='sel'){
	$shout = 'AskId';
}

$Experiment = $level1.'_'.$level2.'_'.$game;
//$Experiment = 'sc_com_0001';
$traderNo =50;

$i=0;
$traderTS = array(); //交易成功次數
$traderS = array(); //喊價次數
$traderTSR = array(); //交易成功率
$traderMig = array(); //交易商遷移次數
$traderMigSta = array(); //交易商遷移穩定度


echo '交易商類型, 交易商編號, 交易成功次數, 喊價次數, 交易成功率, 遷移次數, 交易商遷移穩定度<br />';
while ( $i < $traderNo )
{
	echo $trader.', '.$i.', ';

	//查詢交易成功次數
	$sql="SELECT count(t.Id) AS countId 
	FROM x_shout s, x_trader d, x_transaction t 
	WHERE s.TraderId = '".$trader."_".$i."'
	AND s.ShoutId = t.".$shout."
	AND s.TraderId = d.TraderId 
	AND t.ExperimentId = '".$Experiment."'
	AND t.ExperimentId = s.ExperimentId
	AND t.ExperimentId = d.ExperimentId
	AND s.Status = 'PLACED';";
	
	$result = mysql_query($sql,$link) or die ("Can not read db!".mysql_error());
	
	while($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{		
		$traderTS[$i] = $row['countId'];
		echo $traderTS[$i];
	}

	if($traderTS[$i]!=0){
		//查詢喊價次數
		$shoutSql="SELECT count(s.Id) AS countId 
		FROM x_shout s, x_trader d  
		WHERE s.TraderId = '".$trader."_".$i."'
		AND s.TraderId = d.TraderId 
		AND s.ExperimentId = '".$Experiment."'
		AND d.ExperimentId = s.ExperimentId;";
		
		$shoutResult = mysql_query($shoutSql,$link) or die ("Can not read db!".mysql_error());
	
		while($shoutRow = mysql_fetch_array($shoutResult, MYSQL_ASSOC)) 
		{		
			$traderS[$i] = $shoutRow['countId'];
			$traderTSR[$i] = $traderTS[$i] / $traderS[$i]; //交易成功率 = 交易成功次數 / 喊價次數
			echo ', '.$traderS[$i].', '.$traderTSR[$i].', ';
		}
		
		//查詢交易商市場間遷移次數
		$migSql="SELECT SpecialistId FROM x_shout 
		WHERE TraderId = '".$trader."_".$i."' AND ExperimentId = '".$Experiment."' 
		ORDER BY Id ASC";
		
		$migResult = mysql_query($migSql,$link) or die ("Can not read db!".mysql_error());
		$m=0; //遷移次數
		$SpecialistTempName = '';
		while($migRow = mysql_fetch_array($migResult, MYSQL_ASSOC))
		{	
			if($migRow['SpecialistId'] != $SpecialistTempName){
				$SpecialistTempName = $migRow['SpecialistId'];	
				$m++;
			}
		}
		
		$traderMig[$i] = $m;
		$traderMigSta[$i] = $traderMig[$i] / $traderTSR[$i]; //交易商遷移穩定度 = 市場間移動次數 / 交易成功率 ((穩定度愈高，表示該交易商愈不容易遷移)
		
		echo $traderMig[$i].', '.$traderMigSta[$i].'<br />';
		
	}else{
		$traderS[$i] = 0;
		echo ', 0, 0, 0, 0<br />'; //因為交易成功次數為0 ，因此後面的參數計算便無意義(穩定度為0，同時表示該交易商會連續遷移)。
	}
	
	$i++;
}

echo '</p></body>';
?>
